import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/OrganizationTypeDto.dart';
import 'package:bxultimate/domain/common/MapLatLonDto.dart';
import 'package:bxultimate/domain/common/dict_data.dart';
import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/shared/constant/app_constant.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:bxultimate/widgets/app/app_org_type_select.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class OrganizationCreateController extends GetxController {
  final OrganizationCreateState state = OrganizationCreateState();

  MapLatLonDto mapLatLonDto = MapLatLonDto();
  TextEditingController nameController = TextEditingController();
  TextEditingController reasonController = TextEditingController();

  var isShowLevelSelect = false;


  var pid = Get.arguments;


  @override
  void onInit() {
    super.onInit();

    if(pid !=null){
      state.createDto = state.createDto.copyWith(
        parentId: pid,
      );
    }
  }

  // 地址选择
  Future<void> addressSelect(TDCell cell) async {
    mapLatLonDto = await Get.toNamed(RouterName.commonMapPointSelect);
    state.addressName = mapLatLonDto.address;
    // 获取上报单位
    state.createDto = state.createDto.copyWith(
      address: mapLatLonDto.address,
      lat: mapLatLonDto.lat,
      lon: mapLatLonDto.lon,
      areaXzqh: mapLatLonDto.postcode,
    );
  }

  void createOrg() async{
    if(nameController.text == ''){
      SmartDialog.showNotify(msg: '请填写队伍名称', notifyType: NotifyType.failure);
      return;
    }

    if(state.typeName == ''){
      SmartDialog.showNotify(msg: '请选择队伍类型', notifyType: NotifyType.failure);
      return;
    }
    if(isShowLevelSelect && state.levelName == ''){
      SmartDialog.showNotify(msg: '请选择队伍级别', notifyType: NotifyType.failure);
      return;
    }

    state.createDto = state.createDto.copyWith(
      name: nameController.text,
      content: reasonController.text,
    );

    Result result = await OrganizationRequest.createTeam(state.createDto.toJson());

    if(result.code == AppConstant.RESULT_ERROR){
      SmartDialog.showNotify(msg: result.message!, notifyType: NotifyType.failure);
      return;
    }else {
      SmartDialog.showNotify(msg: '创建队伍成功', notifyType: NotifyType.success);
      Get.back();
    }


  }

  // 民兵的且level 有值的去查
  // 民兵的level 没值的 去选择
  void typeSelect(TDCell cell) {
    UtilMk.openBottomSheet(
      520,
      "户籍所在地选择",
      AppOrgTypeSelect(
        onSelect: (OrganizationTypeDto dto) async {
          state.typeName = dto.name;
          state.createDto = state.createDto.copyWith(
            type: dto.id,
          );
          if (dto.sysType == 'MB') {
            state.showLevel = true;

            if (dto.level == null || dto.level == '-1') {
              isShowLevelSelect = true;
            } else {
              isShowLevelSelect = false;
              // 查询回显
              DictData dictData = await BaseRequest.selectByDicTypeAndValue(
                  'team_level', dto.level);
              state.levelName = dictData.dictLabel;
            }
          } else {
            state.showLevel = false;
          }
          Get.back();
        },
      ),
    );
  }

  void levelSelect(TDCell cell) async{
    if (isShowLevelSelect == false) {
      return;
    }
    List<DictData> dictDataList = await BaseRequest.getDictData('team_level');

    List<String> data = dictDataList.map((e) => e.dictLabel!).toList();
    TDPicker.showMultiPicker(Get.context, title: '选择级别', onConfirm: (selected) {
      state.levelName = data[selected[0]];
      state.createDto = state.createDto.copyWith(
        teamLevel: dictDataList[selected[0]].dictValue
      );
      Get.back();
    }, data: [data]);
  }
}
